import 'dart:io';
import 'dart:typed_data';

import 'package:flutter_chip/utils/log_util.dart';
import 'package:path/path.dart' as path;
import 'package:path_provider/path_provider.dart';
import 'package:sqflite/utils/utils.dart';

import 'date_utils.dart';

class CommUtil {
  static String getTimeDiff(String strTime) {
    int time = DateUtil.getDateMsByTimeStr(strTime);
    LogUtil.v("转换时间：$time");
    int current = DateTime.now().millisecondsSinceEpoch - time;
    double tmp = current / 1000 / 60;
    if (tmp < 1) {
      return "刚刚";
    } else if (tmp < 60) {
      return "${tmp.toInt()}分钟前";
    } else if (tmp < 720) {
      return "${tmp ~/ 60}小时前";
    } else {
      if (DateUtil.getDateStrByMs(DateTime.now().millisecondsSinceEpoch,
              format: DateFormat.YEAR_MONTH_DAY) ==
          DateUtil.getDateStrByMs(time, format: DateFormat.YEAR_MONTH_DAY)) {
        return DateUtil.getDateStrByMs(time, format: DateFormat.HOUR_MINUTE);
      } else {
        return DateUtil.getDateStrByMs(time, format: DateFormat.YEAR_MONTH_DAY);
      }
    }
  }

  static String getMsTime(int time) {
    if (DateUtil.getDateStrByMs(DateTime.now().millisecondsSinceEpoch,
            format: DateFormat.YEAR_MONTH_DAY) ==
        DateUtil.getDateStrByMs(time, format: DateFormat.YEAR_MONTH_DAY)) {
      return DateUtil.getDateStrByMs(time, format: DateFormat.HOUR_MINUTE);
    } else {
      return DateUtil.getDateStrByMs(time, format: DateFormat.ZH_MONTH_DAY);
    }
  }

  static const RollupSize_Units = ["GB", "MB", "KB", "B"];

  /// 返回文件大小字符串
  static String getImageSize(int size) {
    int idx = 3;
    int r1 = 0;
    String result = "";
    while (idx >= 0) {
      int s1 = size % 1024;
      size = size >> 10;
      if (size == 0 || idx == 0) {
        r1 = (r1 * 100) ~/ 1024;
        if (r1 > 0) {
          if (r1 >= 10)
            result = "$s1.$r1${RollupSize_Units[idx]}";
          else
            result = "$s1.0$r1${RollupSize_Units[idx]}";
        } else
          result = s1.toString() + RollupSize_Units[idx];
        break;
      }
      r1 = s1;
      idx--;
    }
    return result;
  }

  ///目标日期和当前日期求相差天数 tagDate:2019-09-30 03:53:41
  static int subDate(String tagDate) {
    int tagTime = DateUtil.getDateMsByTimeStr(tagDate);
    print("tagTime:$tagTime");
    int nowTime = DateUtil.getNowDateMs();
    return (nowTime - tagTime) / 1000 / 60 / 60 ~/ 24;
  }

  static Future<File> saveFile(Uint8List image, {String fileName}) async {
    Directory saveDir = await getApplicationSupportDirectory();
    if (null == fileName) {
      fileName = '${DateTime.now().millisecondsSinceEpoch}.png';
    }
    File saveFile = File("${saveDir.path}${path.separator}$fileName");
    saveFile.createSync(recursive: true);
    saveFile.writeAsBytesSync(image, flush: true);
    return saveFile;
  }

  /// 检查是否是邮箱格式
  static bool isEmail(String input) {
    final String regexEmail =
        "^\\w+([-+.]\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*\$";
    if (input == null || input.isEmpty) return false;
    return new RegExp(regexEmail).hasMatch(input);
  }

  ///二进制转16进制
  static String bin2Hex(List<int> bins) {
    if (bins.length < 8) {
      for (int i = 0; i < 8 - bins.length; i++) {
        bins.add(0);
      }
    }
    int l = 0, h = 0;
    for (int i = 0; i < 4; i++) {
      l += bins[i] * (1 << i);
    }
    for (int i = 4; i < bins.length; i++) {
      print(bins[i] * (1 << (i - 4)));
      h += bins[i] * (1 << (i - 4));
    }
    return '${h.toRadixString(16)}${l.toRadixString(16)}'.toUpperCase();
  }
}
